// 2013-8-1下午06:29:18  net.betou.trade.admin.action.TradeAct.java

package net.betou.trade.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.util.TradeUtils;
import net.betou.trade.action.abs.TradeAct;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Trade;
import net.betou.trade.web.Messages;
import net.betou.trade.web.WebErrors;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author 叶装装
 */
@Controller
public class OpenTradeAct extends TradeAct< Trade >{
	private static final String DIRECTORY = "trade/open";  
	
	@RequestMapping( "/tradeopen/v_list.jspx" )
	public String list( Integer pageSize, Integer pageNo, String filterStatus, String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model  ){
		return super.toList( true, pageSize, pageNo, filterStatus, searchProperty, searchValue, orderProperty, orderDirection, request, response, model );
	}
	
	@RequestMapping( "/tradeopen/v_add.jspx" )
	public String v_add( HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		WebErrors errors = validateToSave( admin );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		model.addAttribute( "groupList", groupMng.getAllowReadList( null, null, null, null ) );
		return getAddPage( );
	}
	
	@RequestMapping( "/tradeopen/o_save.jspx" )
	public String o_save( Trade bean, String categoryName, Integer [ ] userGroup, Integer [ ] smsGroup, HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		
		WebErrors errors = validateSave( bean, categoryName, userGroup );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		bean = manager.saveOpen( admin, categoryName, bean, splitUserGroup( userGroup ), splitUserGroup( smsGroup ));
		
		logMng.operating( 0, request, Messages.LOG_OPEN_TRADE, "id=" + bean.getId( ) );
		
		return "redirect:v_list.jspx";
	}
	
	@RequestMapping ( "/tradeopen/v_edit.jspx" )
	public String v_edit( Integer id, HttpServletRequest request, ModelMap model ){
		return super.toEdit( request, id, model );
	}
	
	@RequestMapping ( "/tradeopen/o_update.jspx" )
	public String o_update( Integer id, String categoryName, Integer [ ] userGroup,  Integer [ ] smsGroup, Trade bean, HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		WebErrors errors = validateOUpdate( id, admin.getSystem( ), admin.getId( ), request );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		// 还可以修改开仓时间
		bean = manager.updateOpen( categoryName, bean, this.splitUserGroup( userGroup ), splitUserGroup( smsGroup ) );
		
		logMng.operating( 1, request, Messages.LOG_OPEN_TRADE, "id=" + bean.getId( ) );
		
		return "redirect:v_list.jspx";
	}
	
	@RequestMapping( "/tradeopen/o_delete.jspx" )
	public void o_delete( Integer [ ] ids, HttpServletResponse response, HttpServletRequest request ){
		super.toDelete( true, ids, response, request );
		return;
		
	}
	
	private String splitUserGroup( Integer [ ] userGroup ){
		String group = "";
		if( userGroup != null ){
			for( int i = 0; i < userGroup.length; i++ ){
				group += userGroup[ i ];
				if( i < userGroup.length - 1 ){
					group += ",";
				}
			}
		}
		
		return group;
	}
	
	private WebErrors validateSave( Trade bean,  String categoryName, Integer [ ] users ){
		WebErrors errors = WebErrors.create( );
		
		if( StringUtils.isBlank( categoryName )){
			errors.addError( Messages.CATEGORY_NOT_EXIST );
			return errors;
		}
		return errors;
	}
	
	private WebErrors validateToSave( AgentAndAdmins admin ){
		WebErrors errors = WebErrors.create( );
//		if( admin.getSystem( ) ){
//			errors.addError( Messages.TRADE_ADMIN_SYSTEM_MANAGER_NOT_TO_ADD );
//		}
		return errors;
	}
	
	@Override
	protected String getDirectory( ) {
		return DIRECTORY;
	}

	
}
